using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;

public class GameManager : MonoSingleton<GameManager>
{
    private const string AUDIO_RELATIVE_PATH = "Audio/";
    private AudioSource _audioSource;
    private IntPtr _pMainWindow;

    protected override void AutoInit()
    {
        _audioSource = FindAnyObjectByType<AudioSource>();
        // HideTaskbarIcon(true);
        SetAutoStart();
        TCPClient.Instance.ConnectServer();
    }

    public void PlayAudio(string audioPath)
    {
        if (!_audioSource)
        {
            UnityEngine.Debug.LogWarning("AudioSource not found");
            return;
        }
        var request = Resources.LoadAsync<AudioClip>(AUDIO_RELATIVE_PATH + audioPath);
        request.completed += (operation) =>
        {
            var audioClip = request.asset as AudioClip;
            _audioSource.Stop();
            _audioSource.PlayOneShot(audioClip);
        };
    }

    public void QuitGame()
    {
        // Camera.main.gameObject.SetActive(false);
        // UniWindowController.current.shouldFitMonitor = false;
        // UniWindowController.current.isZoomed = false;
        // UniWindowController.current.windowSize = new Vector2(400, 300);
        Application.Quit();
    }

    void OnApplicationQuit()
    {
        TCPClient.Instance.Disconnect();
    }

    public List<Action> _actionList = new List<Action>();
    private static readonly object _lock = new object();
    public void RunInMainThread(Action action)
    {
        lock (_lock)
        {
            _actionList.Add(action);
        }
    }

    private void RunActionList()
    {
        lock (_lock)
        {
            foreach (var action in _actionList)
            {
                action?.Invoke();
            }
            _actionList.Clear();
        }
    }

    void Update()
    {
        RunActionList();
    }

    public void HideTaskbarIcon(bool hide)
    {
#if UNITY_EDITOR
        return;
#endif
        const int GWL_EXSTYLE = -20;
        const int WS_EX_APPWINDOW = 0x00040000;
        const int WS_EX_TOOLWINDOW = 0x00000080;

        // 延迟获取窗口句柄（确保窗口已创建）
        if (_pMainWindow == IntPtr.Zero)
        {
            _pMainWindow = GetActiveWindow();
            if (_pMainWindow == IntPtr.Zero) return;
        }

        int style = GetWindowLong(_pMainWindow, GWL_EXSTYLE);
        
        if (hide)
        {
            style = (style & ~WS_EX_APPWINDOW) | WS_EX_TOOLWINDOW;
        }
        else
        {
            style = (style & ~WS_EX_TOOLWINDOW) | WS_EX_APPWINDOW;
        }
        
        SetWindowLong(_pMainWindow, GWL_EXSTYLE, style);
    }

    [DllImport("user32.dll")]
	private static extern IntPtr GetActiveWindow();

	[DllImport("User32.dll")]
	private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

	[DllImport("User32.dll")]
	private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    private void SetAutoStart()
    {
        // 首次启动时设置自启动
        if (PlayerPrefs.GetInt("AutoStart", 0) == 0)
        {
            PlayerPrefs.SetInt("AutoStart", 1);
            PlayerPrefs.Save();
            AutoStartMgr.Instance.SetAutoStart(true);
        }
    }

    public static Dictionary<CV, string> cvMap = new Dictionary<CV, string>
    {
        { CV.Hachiware, "3803" },
        { CV.Usagi, "3808" },
        { CV.Chiikawa, "3801" },
        { CV.Anya, "4403" },
    };
}

public enum CV {
    Hachiware = 0,
    Usagi,
    Chiikawa,
    Anya,
    Count,
    Random = 999,
}